<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control @tabClick="tabClick"
                 ref="tabControl1"
                 class="tab-control" v-show="isTabFixed"/>
    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore">
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends"/>
      <feature-view></feature-view>
      <tab-control @tabClick="tabClick"
                   ref="tabControl2"></tab-control>
      <goods-list :goods="showGoodsList"></goods-list>
    </scroll>
    <!--    当我们监听组件的原生事件时，必须要添加native修饰符，才能监听。
            或者在组件里面定义事件后 发射出来也行-->
    <back-top @click.native="backTop" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import {TOP_DISTANCE, POP, NEW, SELL} from "common/const";
import {getHomeMultidata, getProductData} from "network/home";

import NavBar from "components/common/navbar/NavBar";
import HomeSwiper from "views/home/HomeSwiper";
import RecommendView from "views/home/RecommendView";
import FeatureView from "views/home/FeatureView";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";
import {debounce} from "common/utils";
import {backTopMixin} from "common/mixin";


export default {
  name: "Home",
  components: {
    Scroll,
    GoodsList,
    TabControl,
    FeatureView,
    RecommendView,
    HomeSwiper,
    NavBar
  },
  mixins:[backTopMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        'pop': {page: 1, list: []},
        'new': {page: 1, list: []},
        'sell': {page: 1, list: []},
      },
      currentType: POP,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0
    }
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.refresh()
  },
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY()
  },
  computed: {
    showGoodsList() {
      return this.goods[this.currentType].list
    }
  },
  created() {
    // 1.请求轮播等数据
    this.getMultidata()

    //2.请求商品数据
    this.getProductData(POP);
    this.getProductData(NEW);
    this.getProductData(SELL);
  },
  mounted() {
    // 1.图片加载完成的事件监听
    const refresh = debounce(this.$refs.scroll.refresh, 50)
    this.$bus.$on('itemImageLoad', () => {
      refresh()
    })
  },
  methods: {
    /**
     * 事件监听相关的方法
     */
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = POP
          break
        case 1:
          this.currentType = NEW
          break
        case 2:
          this.currentType = SELL
          break
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    contentScroll(position) {
      // 1.判断BackTop是否显示
      // this.isShowBackTop = (-position.y) > BACK_POSITION
          this.listenShowBackTop(position);
      // 2.决定tabControl是否吸顶(position: fixed)
      this.isTabFixed = (-position.y) > this.tabOffsetTop
    },
    loadMore() {
      this.getProductData(this.currentType)
      //TODO:已从GoodsListItem组件中发送图片加载完成事件至EventBus中
      //在mounted事件里监听
      // this.$refs.scroll.scroll.refresh()
    },
    /**
     * 网络请求
     */
    getMultidata() {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list
        this.recommends = res.data.recommend.list
      })
    },
    getProductData(type) {
      //获取页码
      const page = this.goods[type].page;
      getProductData(type, page).then(res => {
        const newList = res.data.list;
        this.goods[type].list.push(...newList);
        this.goods[type].page += 1;

        //完成加载更多数据
        this.$refs.scroll.finishPullUp();
      })
    },
    /**
     * 用于记录导航吸顶的位置
     */
    swiperImageLoad() {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    },
  }

}
</script>

<style scoped>
#home {
  height: 100vh;
  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /*在使用浏览器原生滚动时才会起用*/
  /*position: fixed;*/
  /*left: 0;*/
  /*right: 0;*/
  /*top: 0;*/
  /*z-index: 9;*/
}

.tab-control {
  /*滚动时达高度时再定位不再滚动*/
  /*position: sticky;*/
  position: relative;
  z-index: 9;
}

.content {
  overflow: hidden;
  position: absolute;
  top: 44px;
  bottom: 58px;
  left: 0;
  right: 0;
}
</style>
